<?php
namespace app\system\controller;

/**
 * 提现管理
 * */
 
class Evaluate extends Common
{

    protected $pageSize = 20;

    /**
     * 列表
     * @param string $keywords
     * @return mixed
     * @throws \think\exception\DbException
     */
	public function index($keywords='')
	{
        $where = [];
        !empty($keywords) && $where['u.name|u.tel|g.title|e.content'] = ['like', "%$keywords%"];

        $pageconfig['path'] = url("?keywords={$keywords}");
        $evaluate_model = new \app\common\model\Evaluate();
        $list = $evaluate_model->pageListJoinGoods($where, $this->pageSize, $pageconfig, 'e.*,u.name,u.tel,g.title g_title', 'e.id desc');

        $this->assign('list', $list);
        return $this->fetch();
	}

    /**
     * 删除
     * @param int $id
     * @return \think\response\Json
     */
    public function del($id)
    {
        if (isAjaxPost()) {
            $id = intval($id);
            if ($id <= 0) {
                return $this->jsonError('删除失败');
            }

            $model = new \app\common\model\Evaluate();
            if ($model->countNum(['id'=>$id,'status'=>2]) <= 0) {
                return $this->jsonError('删除失败');
            }

            if ($model->del(['id'=>$id]) !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }

    /**
     * 回复评价
     * @param int $id
     * @param string $reply
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function examine ($id, $reply='') {
        if (isAjaxPost()) {
            if ($id <= 0) {
                return $this->jsonError('缺少必要参数');
            }
            $model = new \app\common\model\Evaluate();
            $info = $model->findInfo($id);
            if (empty($info)) {
                return $this->jsonError('评价不存在');
            }
            if ($info['replay_time'] > 0) {
                return $this->jsonError('您已经回复过了');
            }
            $reply = trim($reply);

            if (empty($reply)) {
                return $this->jsonError('请输入回复内容');
            }
            $data = [
                'reply' => $reply,
                'reply_time' => time(),
                'manage_id' => session('manageInfo.id')
            ];

            if ($model->save($data, ['id'=>$id]) !== false) {
                return $this->jsonSuccess('回复成功');
            } else {
                return $this->jsonError('回复失败');
            }
        }
    }
}
